package sit.engreal.leetcode.mianshi;

import java.util.HashMap;

/**
 * <a href="https://leetcode.cn/problems/check-permutation-lcci/">面试题 01.02. 判定是否互为字符重排</a>
 */
public class T01T02 {
    public boolean CheckPermutation(String s1, String s2) {
        if (s1.length() != s2.length()) {
            return false;
        }
        final HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s1.length(); i++) {
            final char ch = s1.charAt(i);
            map.put(ch, map.getOrDefault(ch, 0) + 1);
        }
        for (int i = 0; i < s2.length(); i++) {
            final char ch = s2.charAt(i);
            final int n = map.getOrDefault(ch, 0);
            if (n == 0) {
                return false;
            } else {
                map.put(ch, n - 1);
            }
        }
        return true;
    }
}
